﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Util;

namespace TcpClientApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private TcpClient Client;
        private void Init()
        {
            Client = new TcpClient();
            Client.HostName = "127.0.0.1";
            Client.Port = 3000;
            Client.DataReceived += Client_DataReceived;
            Client.ConnectionStatusChanged += Client_ConnectionStatusChanged;

        }

        private delegate void DelMethod(object sender, TcpDataReceivedEventArgs e);

        private void Client_DataReceived(object sender, TcpDataReceivedEventArgs e)
        {
            //收到数据
            if (InvokeRequired)
            {
                BeginInvoke(new DelMethod(Client_DataReceived), new object[] { sender, e });
                return;
            }

            if (chkHex.Checked)
            {
                //16进制方式
                txtReceived.Text = txtReceived.Text + DataConv.ToHexStr(e.Data);
            }
            else
            {
                //字符串方式
                txtReceived.Text = txtReceived.Text + e.GetString();
            }

        }

        private void Client_ConnectionStatusChanged(object sender, EventArgs e)
        {
            //连接状态改变
            if (InvokeRequired)
            {
                BeginInvoke(new DelMethod(Client_ConnectionStatusChanged), new object[] { sender, e });
                return;
            }

            ShowConnectState();
        }

        void ShowConnectState()
        {
            this.Text = string.Format("TcpClient [{0}:{1}], {2}", Client.HostName, Client.Port, Client.Connected ? "已连接" : "未连接");
            btnConnect.Text = Client.Connected ? "断开" : "连接";

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Init();
            btnConnect.PerformClick();          
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Client.Disconnect();
            Client.Dispose();
            Client = null;
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            //连接/断开

            try
            {

                if (!Client.Connected)
                {
                    Client.HostName = txtIP.Text.Trim();
                    Client.Port = int.Parse(txtPort.Text.Trim());
                    Client.Connect();
                }
                else
                {
                    Client.Disconnect();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            //发送
            if (chkHex.Checked)
            {
                //16进制方式
                byte[] buffer = DataConv.FromHexStr(txtSend.Text.Trim());
                Client.Write(buffer);
            }
            else
            {
                //字符串方式
                Client.Write(txtSend.Text.Trim());
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            //清空
            txtSend.Clear();
            txtReceived.Clear();
        }

        private void txtSend_KeyDown(object sender, KeyEventArgs e)
        {
            if((e.Control || e.Alt) && e.KeyCode == Keys.Return)
            {
                btnSend.PerformClick();
            }
        }
    }
}
